package db

const (
	defaultMaxOpenConnections = 10
	defaultMaxIdleConnections = 5
)

type Options struct {
	MaxIdleConns          int
	MaxOpenConns          int
	ReadTimeout           string
	WriteTimeout          string
	SSLMode               string
	LogMode               bool
	DisableSimpleProtocol bool
}

func NewOptions(opts ...Option) Options {
	o := Options{
		MaxOpenConns: defaultMaxOpenConnections,
		MaxIdleConns: defaultMaxIdleConnections,
		ReadTimeout:  "10s",
		WriteTimeout: "10s",
	}

	for _, opt := range opts {
		opt(&o)
	}

	return o
}

// Option is a function to set the db options.
type Option func(*Options)

// WithMaxOpenConnections let you set the max number of connection that could be opened.
func WithMaxOpenConnections(n int) Option {
	return func(o *Options) {
		o.MaxOpenConns = n
	}
}

// WithMaxIdleConnections let you set the max number of idle connections.
func WithMaxIdleConnections(n int) Option {
	return func(o *Options) {
		o.MaxIdleConns = n
	}
}

//WithReadTimeout I/O write timeout. The value must be a decimal number with a unit suffix ("ms", "s", "m", "h"), such as "30s", "0.5m" or "1m30s".
func WithReadTimeout(value string) Option {
	return func(o *Options) {
		o.ReadTimeout = value
	}
}

//WithWriteTimeout I/O read timeout. The value must be a decimal number with a unit suffix ("ms", "s", "m", "h"), such as "30s", "0.5m" or "1m30s".
func WithWriteTimeout(value string) Option {
	return func(o *Options) {
		o.WriteTimeout = value
	}
}

//WithSslmode sslmode. The value must be a string of:
//* disable - No SSL
//* require - Always SSL (skip verification)
//* verify-ca - Always SSL (verify that the certificate presented by the server was signed by a trusted CA)
//* verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name  matches the one in the certificate)
func WithSslmode(value string) Option {
	return func(o *Options) {
		o.SSLMode = value
	}
}

// WithLogMode logMode.
// - true: open log mode
// - false: close log model
func WithLogMode(value bool) Option {
	return func(o *Options) {
		o.LogMode = value
	}
}

func DisableSimpleProtocol() Option {
	return func(o *Options) {
		o.DisableSimpleProtocol = true
	}
}
